package com.yuma.springshiroredisthymeleaf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuma.springshiroredisthymeleaf.common.ResultEnum;
import com.yuma.springshiroredisthymeleaf.common.ResultInfo;
import com.yuma.springshiroredisthymeleaf.entity.Goods;
import com.yuma.springshiroredisthymeleaf.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    // key: getAll_1_2 :  goods
    // key: getAll_2_2 : goods
//    @RequiresRoles("admin")
    @RequestMapping("/getAll/{page}/{pageSize}")
//    @RequiresRoles("admin")
//    @RequiresPermissions("goods:*")
    @RequiresAuthentication  // 登录
    @ResponseBody
//    @Cacheable(value = "goods", key = "'getAll_' + #page + '_' + #pageSize")
    public ResultInfo getAll(@PathVariable("page") int page,
                             @PathVariable("pageSize") int pageSize, HttpServletRequest request) throws SQLException {
        //分页
        PageHelper.startPage(page,pageSize);
        List<Goods> goods = goodsService.selectByCondition(null);
        PageInfo pageInfo=new PageInfo(goods);
        ResultInfo ri = new ResultInfo();
        ri.setCode(ResultEnum.SUCCESS.getCode());
        ri.setMessage(ResultEnum.SUCCESS.getMsg());
        ri.setResult(pageInfo.getList());
        request.setAttribute("resultInfo",ri);
        return ri;
    }

    @RequestMapping("/index")
    public String index() throws SQLException {
        return "goods";
    }
}